SET NOCOUNT ON;
declare @version smallint = 1;
declare @release smallint = 1;
declare @script_name varchar(256)
set @script_name='UtilityDB_V'+convert(varchar(5),@version)+'R'
+right('00000'+convert(varchar(5),@release),5)+'.sql'
begin try
begin tran
if not exists (select 1 from sys.tables where name='database_version_information' and schema_id=schema_id('dbo')) 
BEGIN
/*--Begin Code Here --*/

	create table dbo.database_version_information (
	script_name varchar(256) not null
	,version_number smallInt not null
	,release_number smallInt not null
	,date_released datetime not null
	,undone bit not null
	,constraint pk_database_version_information__script_name primary key (script_name)
	)	
	insert into dbo.database_version_information 
	values(@script_name,@version,@release,GETDATE(),0);

/*--End Code Here --*/
END
ELSE
	update dbo.database_version_information 
	set undone=0
	where
	script_name =@script_name;
print 'script succeeded!'
commit tran
end try
begin catch 
	declare @error_message varchar(2000);
	set @error_message='SCRIPT:'+@script_name+' FAILED! ROLLBACK!  ERROR_MESSAGE:'
	+coalesce(error_message(),'')+' ERROR_NUMBER:'+coalesce(convert(varchar(10),error_number()),'');
	ROLLBACK
	raiserror(@error_message,16,1)
end catch

